package com.dhemery.mack.model;

import java.util.ArrayList;
import java.util.List;

//TODO Move event manager stuff into separate class.
public class Item {
	private static List<ItemChangeListener> nameChangeListeners = new ArrayList<ItemChangeListener>();
	
	public static void subscribeToNameChange(ItemChangeListener newSubscriber) {
		if (!nameChangeListeners.contains(newSubscriber)) nameChangeListeners.add(newSubscriber);
	}

	public static void unsubscribeFromNameChange(ItemChangeListener oldSubscriber) {
		nameChangeListeners.remove(oldSubscriber);
	}

	private String description = "";
	private String name;

	public Item(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public String getName() {
		return name;
	}

	private void publishNameChange() {
		for(ItemChangeListener listener : nameChangeListeners)
			listener.nameChanged(this);
	}

	public void setDescription(String newDescription) {
		description = newDescription;
	}

	public void setName(String newName) {
		if(newName.equals(name)) return;
		name = newName;
		publishNameChange();
	}
}